<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务管理系统 - 任务列表</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入自定义样式 -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('dashboard') }}">
                <i class="fa fa-tasks mr-2"></i>任务管理系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="切换导航">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('dashboard') }}">
                            <i class="fa fa-dashboard mr-1"></i>仪表盘
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="{{ url_for('task_list') }}">
                            <i class="fa fa-list mr-1"></i>任务列表
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('category_list') }}">
                            <i class="fa fa-folder mr-1"></i>分类管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('reports') }}">
                            <i class="fa fa-bar-chart mr-1"></i>数据报表
                        </a>
                    </li>
                </ul>
                <div class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fa fa-user-circle mr-1"></i>{{ session.username }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                            <li>
                                <a class="dropdown-item" href="{{ url_for('notifications') }}">
                                    <i class="fa fa-bell mr-1"></i>通知
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('settings') }}">
                                    <i class="fa fa-cog mr-1"></i>设置
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('logout') }}">
                                    <i class="fa fa-sign-out mr-1"></i>退出登录
                                </a>
                            </li>
                        </ul>
                    </li>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <div class="container-fluid mt-4">
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="关闭"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <!-- 页面标题和操作按钮 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>
                <i class="fa fa-list mr-2"></i>任务列表
            </h2>
            <div class="d-flex gap-2">
                <a href="{{ url_for('add_task') }}" class="btn btn-primary">
                    <i class="fa fa-plus-circle mr-1"></i>新建任务
                </a>
                <div class="btn-group">
                    <button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fa fa-filter mr-1"></i>筛选
                    </button>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="{{ url_for('task_list', status='todo') }}">待办任务</a></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list', status='in_progress') }}">进行中任务</a></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list', status='completed') }}">已完成任务</a></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list', status='archived') }}">已归档任务</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list', priority='high') }}">高优先级</a></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list', priority='medium') }}">中优先级</a></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list', priority='low') }}">低优先级</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="{{ url_for('task_list') }}">全部任务</a></li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- 搜索和筛选区域 -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="GET" action="{{ url_for('task_list') }}" class="row g-3">
                    <div class="col-md-4">
                        <div class="input-group">
                            <span class="input-group-text">
                                <i class="fa fa-search"></i>
                            </span>
                            <input type="text" class="form-control" name="search" placeholder="搜索任务标题或描述" value="{{ search_term.strip('%') if search_term else '' }}">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="status">
                            <option value="all" {% if current_status == 'all' %}selected{% endif %}>全部状态</option>
                            <option value="todo" {% if current_status == 'todo' %}selected{% endif %}>待办</option>
                            <option value="in_progress" {% if current_status == 'in_progress' %}selected{% endif %}>进行中</option>
                            <option value="completed" {% if current_status == 'completed' %}selected{% endif %}>已完成</option>
                            <option value="archived" {% if current_status == 'archived' %}selected{% endif %}>已归档</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="priority">
                            <option value="all" {% if current_priority == 'all' %}selected{% endif %}>全部优先级</option>
                            <option value="high" {% if current_priority == 'high' %}selected{% endif %}>高</option>
                            <option value="medium" {% if current_priority == 'medium' %}selected{% endif %}>中</option>
                            <option value="low" {% if current_priority == 'low' %}selected{% endif %}>低</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="category">
                            <option value="all" {% if current_category == 'all' %}selected{% endif %}>全部分类</option>
                            {% for category in categories %}
                                <option value="{{ category.id }}" {% if current_category == category.id|string %}selected{% endif %}>{{ category.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fa fa-filter mr-1"></i>应用筛选
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- 任务列表 -->
        <div class="card">
            <div class="card-header bg-white border-bottom border-gray-200">
                <h4 class="card-title mb-0">任务列表</h4>
            </div>
            <div class="card-body p-0">
                {% if tasks %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="bg-light">
                                <tr>
                                    <th scope="col">标题</th>
                                    <th scope="col">分类</th>
                                    <th scope="col">优先级</th>
                                    <th scope="col">状态</th>
                                    <th scope="col">截止日期</th>
                                    <th scope="col">创建时间</th>
                                    <th scope="col">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for task in tasks %}
                                    <tr>
                                        <td>
                                            <a href="{{ url_for('view_task', task_id=task.id) }}" class="text-decoration-none text-dark">
                                                {{ task.title }}
                                            </a>
                                        </td>
                                        <td>
                                            {% if task.category %}
                                                <span class="badge" style="background-color: {{ task.category.color }}">{{ task.category.name }}</span>
                                            {% else %}
                                                <span class="text-muted">无分类</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <span class="badge bg-{{ 'danger' if task.priority == 'high' else 'warning' if task.priority == 'medium' else 'success' }}">
                                                {{ task.priority | capitalize }}
                                            </span>
                                        </td>
                                        <td>
                                            <span class="badge bg-{{ 'secondary' if task.status == 'todo' else 'info' if task.status == 'in_progress' else 'success' if task.status == 'completed' else 'dark' }}">
                                                {{ task.status | capitalize }}
                                            </span>
                                        </td>
                                        <td>
                                            {% if task.due_date %}
                                                {{ task.due_date.strftime('%Y-%m-%d %H:%M') }}
                                                {% set days_left = (task.due_date - datetime.utcnow()).days %}
                                                {% if days_left < 1 and task.status != 'completed' and task.status != 'archived' %}
                                                    <span class="text-danger ml-1">(今天)</span>
                                                {% elif days_left < 2 and task.status != 'completed' and task.status != 'archived' %}
                                                    <span class="text-warning ml-1">(明天)</span>
                                                {% endif %}
                                            {% else %}
                                                <span class="text-muted">无截止日期</span>
                                            {% endif %}
                                        </td>
                                        <td>{{ task.created_at.strftime('%Y-%m-%d') }}</td>
                                        <td>
                                            <div class="btn-group" role="group">
                                                <a href="{{ url_for('view_task', task_id=task.id) }}" class="btn btn-sm btn-info">
                                                    <i class="fa fa-eye"></i>
                                                </a>
                                                <a href="{{ url_for('edit_task', task_id=task.id) }}" class="btn btn-sm btn-primary">
                                                    <i class="fa fa-pencil"></i>
                                                </a>
                                                <button type="button" class="btn btn-sm btn-danger delete-task" data-task-id="{{ task.id }}">
                                                    <i class="fa fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                {% else %}
                    <div class="text-center text-muted py-8">
                        <i class="fa fa-inbox fa-4x mb-3"></i>
                        <p class="h5">暂无任务记录</p>
                        <p>没有找到符合条件的任务</p>
                        <a href="{{ url_for('add_task') }}" class="btn btn-primary mt-2">创建第一个任务</a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 删除确认模态框 -->
    <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="关闭"></button>
                </div>
                <div class="modal-body">
                    确定要删除这个任务吗？此操作不可恢复。
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <form id="deleteForm" method="POST" style="display: inline;">
                        <button type="submit" class="btn btn-danger">确认删除</button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入自定义JS -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>

    <script>
        // 删除任务功能
        document.addEventListener('DOMContentLoaded', function() {
            const deleteButtons = document.querySelectorAll('.delete-task');
            const deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
            const deleteForm = document.getElementById('deleteForm');
            
            deleteButtons.forEach(button => {
                button.addEventListener('click', function() {
                    const taskId = this.getAttribute('data-task-id');
                    deleteForm.action = `/tasks/${taskId}/delete`;
                    deleteModal.show();
                });
            });
        });
    </script>
</body>
</html>